// pages/send/send.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: [],
        index: 0,
        date: '2016-09-01',
	    	img:null,
	    	flag: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var $this = this;
        var date = util.formatDate(new Date());
        this.setData({
            date: date
        });
        wx.request({
            url: app.globalData.baseURL + 'Info/types',
            method: 'POST',
            data: {},
            header: {

            },
            success: function(res) {
                // console.log(res.data);
                $this.setData({
                    array: res.data
                })
            }
        });
    },

    formSubmit: function(e) {
		var $this = this;
        console.log('form发生了submit事件，携带数据为：', e.detail.value)
		let tid = this.data.array[e.detail.value.type].tId;
		let price = e.detail.value.price;
		let title = e.detail.value.title;
		console.log(tid);
		console.log(price);
		console.log(title);
		if(price!=""&&title!=""){
			wx.uploadFile({
				url: app.globalData.baseURL + 'Action/doSend',
				filePath: this.data.img[0],
				name: 'pic',
				formData: {
					'uid': app.globalData.userInfo.userId,
					// 'uid': 1,
					'gTitle': e.detail.value.title,
					'detail': e.detail.value.gdetail,
					'typeid': tid,
					'tags': e.detail.value.tags,
					'price': e.detail.value.price,
					'vdate': e.detail.value.date
				},
				success: function (res) {
					console.log(res);
					if(res.data==1){
						wx.navigateBack({
							delta: 1
						});
					}else{
						wx.showToast({
							title: '发送失败',
							icon: 'none',
							duration: 1000
						})
					}
				}
			})
		}else{
			wx.showToast({
				title: '求求你填完表单好吧!',
				icon: 'none',
				duration: 1000
			});
		}
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindPickerChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value
        })
    },
    bindDateChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
    },
	chooseImage: function (e){
		var $this=this;
		wx.showLoading({
			title: '...',
		});
		wx.chooseImage({
			count: 1,
			sizeType: ['original','compressd'],
			sourceType: ['album','camera'],
			success: function(res) {
				const tempFilePaths = res.tempFilePaths;
				console.log(tempFilePaths);
				$this.setData({
					flag: !$this.data.flag,
					img: tempFilePaths
				});
			},
			fail: function(res) {},
			complete: function(res) {
				wx.hideLoading();
			},
		});
	}
})